# delimit ;
clear ;
set more off ;
est drop _all ;
cd "R:/personlig/fenellac/menarche_replication/analysis/" ;

global plusdir "R:/personlig/fenellac/stata_ado/plus/" ;
sysdir set PLUS $plusdir ;
adopath ++ $plusdir ;

* **************************************************************************** ;
* This code replicates Appendix F, Table 2.
*
* Paper: "Age of Marriage and Women's Political Engagement: Evidence from India"
* Authors: Fenella Carpena, Francesca Jensenius
* 
* Code by Fenella Carpena
* Last update: April 28, 2020
* **************************************************************************** ;

****************************************************************************** ;
* select the sample
****************************************************************************** ;

use "./input/eligible-women-ihds02-lhs.dta", clear ;
merge 1:1 IDHH PERSONID using "./input/eligible-women-ihds02-rhs.dta" ;
tab _merge ;
assert _merge == 3 ;
drop _merge ;

* keep only rural women ;
keep if rural == 1 ;

* keep only women who are married only once (i.e., have not ever remarried) ;
drop if ever_remarried == 1 ;
  
* keep only women age of menarche between 11-18 (1st-99th pctile) ; 
summ menarche_age ;
keep if menarche_age >= 11 & menarche_age <= 18 ;

* generating district identifier ;
egen dt_id = group(STATEID DISTID) ;

****************************************************************************** ;
* set-up data for analysis 
****************************************************************************** ;

* rename these variables to make it shorter ;
ren num_children_with nchild ;
ren years_schooling yrssch ;
ren discuss_politics discuss ;
ren member_pol_org member ;

* label variables ; 
label var attended "Attended village council meeting last year" ;
label var discuss "Discusses politics and community with husband" ;
label var member "Participates in a political organization" ;
label var yrssch "\shortstack[c]{Years \\ education}" ;
label var nchild "\shortstack[c]{No. of children \\ living with \\ respondent}" ;
label var menarche_age "Menarche age" ;

* make sure the sample is the same across all regressions 
* i.e., remove any observation which has missing values for the relevant variables, 
* because otherwise, each regression has a different sample and the regressions
* will not be comparable ; 
foreach var of varlist 
attended discuss member
yrssch nchild
menarche_age 
height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ {;
	drop if `var' == . ;
} ; 

* remove district FEs manually to ease estimation later ; 
foreach var of varlist 
attended discuss member
yrssch nchild
menarche_age 
height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ { ;
	qui reg `var' i.dt_id  ;
	predict r`var', resid ; 
} ;

* control vars ;
local rcvars "rheight_measure rage rhindu rmuslim rsc rst robc rmother_yrs_educ rfather_yrs_educ" ;

****************************************************************************** ;
* Panel A: Effect of menarche age on mediators 
****************************************************************************** ;
est drop _all ;

areg yrssch menarche_age height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto yrssch ; 

areg nchild menarche_age height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto child ; 

esttab * using "./output/table-mediation-analysis-panel-a.tex", 
	replace noobs
	drop(height_measure hindu muslim sc st obc age mother_yrs_educ father_yrs_educ _cons) 
	cells(b(label() star fmt(%9.4f %9.4f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	prehead(\begin{table}[t] \vspace{-8ex} \footnotesize `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"'
		\captionsetup{justification=centering} 
		\caption{Mediation Analysis, Rural Women}
		\label{table-mediation-analysis}
		\begin{subtable}[p]{0.5\linewidth}
		\caption*{Panel A: Effect of Menarche Age on Mediator\hspace*{\fill}}
		\vspace{-6ex}
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
		\begin{tabular*}{\hsize}{p{6cm}p{2cm}p{2cm}}
		\label{table-mediation-analysis-panel-a} \\ 
		\toprule)
	legend label  booktabs  collabels( , none)
	mlabels(, depvar span prefix(\multicolumn{@span}{p{2cm}}{) suffix(}))
	postfoot(`"\midrule"'  \end{tabular*} \end{subtable} \hspace*{\fill} \\) ;
	
****************************************************************************** ;
* Panel B: Effect of menarche age and mediator on outcomes
****************************************************************************** ;
est drop _all ; 

areg attended menarche_age yrssch height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto attended1 ; 

areg attended menarche_age nchild height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto attended2 ; 

areg discuss menarche_age yrssch height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto discuss1 ; 

areg discuss menarche_age nchild height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto discuss2 ;

areg member menarche_age yrssch height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto member1 ; 

areg member menarche_age nchild height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ, a(dt_id) robust ;
est sto member2 ; 

* relabel variables for the table ;
label var yrssch "Years education" ;
label var nchild "No. of children living with respondent" ;

esttab * using "./output/table-mediation-analysis-panel-b.tex", 
	replace noobs
	drop(height_measure hindu muslim sc st obc age mother_yrs_educ father_yrs_educ _cons) 
	cells(b(label() star fmt(%9.4f %9.4f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	prehead(\begin{subtable}[p]{\linewidth} 
		\vspace{2ex}
		\caption*{Panel B: Effect of Menarche Age and Mediator on Outcomes\hspace*{\fill}}
		\vspace{-6ex}
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
		\begin{tabular*}{\hsize}{p{6cm}p{2cm}p{2cm}p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}}
		\label{table-mediation-analysis-panel-b} \\ 
		\midrule)
	nodepvars nomtitles
	legend label  booktabs  collabels( , none)
	mgroups(
	"\shortstack[c]{Attended village council\\ meeting last year}"
	"\shortstack[c]{Discusses politics and \\ community with husband}" 
	"\shortstack[c]{Participates in a \\ political organization}" 
	, pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	postfoot(`"\midrule"'  \end{tabular*} \end{subtable}) ;
		
****************************************************************************** ;
* Panel C: ACME Estimates
****************************************************************************** ;
capture log close ;
log using "./temp/results-for-table-mediation-analysis-panel-c.log", text replace ;

* set to Stata version 12 so that the sequence of random numbers based on the
* seed is the same as in the paper (which uses version 12) ;
version 12 ;

****************************************************************************** ;
* Y: attended meeting, M: years_schooling 
****************************************************************************** ;

local mediator "yrssch" ;
local yvar "attended" ; 
local treat "menarche_age" ;

medeff	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(1000) seed(3910) vce(robust) ;
		
****************************************************************************** ;
* Y: attended meeting, M: # of children living w/ respondent 
****************************************************************************** ;

local mediator "nchild" ;
local yvar "attended" ; 
local treat "menarche_age" ;

medeff	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(1000) seed(3910) vce(robust) ;

****************************************************************************** ;
* Y: discusses politics at home,  M: years schooling
****************************************************************************** ;

local mediator "yrssch" ;
local yvar "discuss" ; 
local treat "menarche_age" ;

medeff	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(1000) seed(3910) vce(robust) ;

****************************************************************************** ;
* Y: discusses politics at home,  M: # of children living w/ respondent 
****************************************************************************** ;

local mediator "nchild" ;
local yvar "discuss" ; 
local treat "menarche_age" ;

medeff	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(1000) seed(3910) vce(robust) ;

****************************************************************************** ;
* Y: member pol org,  M: years schooling
****************************************************************************** ;

local mediator "yrssch" ;
local yvar "member" ; 
local treat "menarche_age" ;

medeff	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(1000) seed(3910) vce(robust) ;

****************************************************************************** ;
* Y: member pol org,  M: # of children living w/ respondent 
****************************************************************************** ;

local mediator "nchild" ;
local yvar "member" ; 
local treat "menarche_age" ;

medeff	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(1000) seed(3910) vce(robust) ;

log close ;
exit ;
